package com.example.ticket.controller;

import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

@RequestMapping("/common")
@Controller
public class CommonController {
    String prefix="common";

    @GetMapping("/myList")
    public String userList(Model model){
        SecurityContext context= SecurityContextHolder.getContext();
        System.out.println("userDetails:"+context);
        Authentication authentication=context.getAuthentication();
        UserDetails principal=(UserDetails)authentication.getPrincipal();
        model.addAttribute("username",principal.getUsername());
        return prefix+"/myList";
    }

    @GetMapping("/home")
    public String Home(Model model){
        SecurityContext context= SecurityContextHolder.getContext();
        System.out.println("userDetails:"+context);
        Authentication authentication=context.getAuthentication();
        UserDetails principal=(UserDetails)authentication.getPrincipal();
        model.addAttribute("username",principal.getUsername());
        return prefix+"/home";
    }

    @GetMapping("/mytest")
    public String Test(Model model){
        SecurityContext context= SecurityContextHolder.getContext();
        System.out.println("userDetails:"+context);
        Authentication authentication=context.getAuthentication();
        UserDetails principal=(UserDetails)authentication.getPrincipal();
        model.addAttribute("username",principal.getUsername());
        return prefix+"/mytest";
    }


    @GetMapping("/addTicket")
    public String addTicket(Model model){
        SecurityContext context= SecurityContextHolder.getContext();
        System.out.println("userDetails:"+context);
        Authentication authentication=context.getAuthentication();
        UserDetails principal=(UserDetails)authentication.getPrincipal();
        model.addAttribute("username",principal.getUsername());
        return prefix+"/addTicket";
    }

}
